package com.keepercloud.qqchat.repository;

import com.keepercloud.qqchat.entity.User;
import com.keepercloud.qqchat.entity.UserList;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author liyk
 * @date 2019/2/12
 */
@Repository
public interface UserListRepository extends JpaRepository<UserList, Integer> {


    /**
     * 获取主页用户列表
     * @param username
     * @return
     */
    @Query(value =
            " select '' as icon, username,'' as lastMsgTime,0 as unReadMsgCount, '测试' as unReadMsg " +
            " from user a where a.username != ?1 "
            ,nativeQuery = true)
    List<Map> getUserList(String username);


    /**
     * 获取主页用户列表
     * @param username
     * @return
     */
    @Query(value = "select null as icon, user_list_name,null as lastMsgTime,0 as nureadMsgCount, null as unreadMsg  from user_list where user_group_id is null " +
            " union " +
            " select null as icon, user_list_name,null as lastMsgTime,0 as nureadMsgCount, null as unreadMsg from user_list a " +
            " where user_group_id is not null " +
            " and exists (select username from user_group_detail b where username = ?1 and a.user_group_id = b.user_group_id)"
            ,nativeQuery = true)
    List<Map> getUserList2(String username);


}